package dyyx;

import java.util.Random;

import org.apache.commons.math3.stat.descriptive.moment.Kurtosis;
import org.apache.commons.math3.stat.descriptive.moment.Mean;
import org.apache.commons.math3.stat.descriptive.moment.Skewness;
import org.apache.commons.math3.stat.descriptive.moment.StandardDeviation;
import org.apache.commons.math3.stat.descriptive.moment.Variance;
import org.apache.commons.math3.stat.descriptive.rank.Max;
import org.apache.commons.math3.stat.descriptive.rank.Min;
import org.apache.commons.math3.stat.descriptive.rank.Percentile;
import org.apache.commons.math3.stat.descriptive.summary.Sum;
import org.apache.commons.math3.stat.descriptive.summary.SumOfSquares;

public class Hello {
	
	
	private static final Random r = new Random();
	
	public static void main(String[] args) throws Exception {
		
		System.out.println("hello,mathdemo");
		
		// r.nextGaussian();
		double[] values = new double[] {0.1 , 0.2 , 0.3 , 0.4 , 0.5 , 0.6 , 0.7,0.55,0.47,0.33,0.21,0.09};
		
		Min min = new Min(); 
        Max max = new Max(); 
        Mean mean = new Mean(); 
        Sum sum = new Sum(); 
        Variance variance = new Variance(); 
        Percentile percentile = new Percentile(); 
        Skewness skewness = new Skewness(); 
        Kurtosis kurtosis = new Kurtosis();  
        SumOfSquares sumOfSquares = new SumOfSquares(); 
        StandardDeviation standardDeviation =new StandardDeviation(); 

		System.out.println("min="+min.evaluate(values));
		System.out.println("max="+max.evaluate(values));
		System.out.println("mean="+mean.evaluate(values));
		System.out.println("sum="+sum.evaluate(values));
		System.out.println("variance="+variance.evaluate(values));
		System.out.println("percentile 80% ="+percentile.evaluate(values,0.8));
		System.out.println("percentile 50% ="+percentile.evaluate(values,0.5));
		System.out.println("percentile 30% ="+percentile.evaluate(values,0.3));

		System.out.println("sumOfSquares="+sumOfSquares.evaluate(values));
		System.out.println("standardDeviation="+standardDeviation.evaluate(values));

		System.out.println("skewness="+skewness.evaluate(values));
		System.out.println("kurtosis="+kurtosis.evaluate(values));
		
		
		System.out.println("\n\n\n");
		// 正态分布 随机数
		
		int num = 10000;
		values = new double[num];
		double value = 0;
		for(int i=0;i<num;i++){
			value =  r.nextGaussian();
			values[i] = value;
			// System.out.print(value+",");
		}
		System.out.println();
		System.out.println("mean="+mean.evaluate(values));
		System.out.println("variance="+variance.evaluate(values));
		System.out.println("standardDeviation="+standardDeviation.evaluate(values));
		System.out.println("skewness="+skewness.evaluate(values));
		System.out.println("kurtosis="+kurtosis.evaluate(values));
		
		
		System.out.println("\n\n\n");
		values = new double[] {1,2,3,4,5,6,7,4,3,2,1};
		
		double meanValue = mean.evaluate(values);
		double varianceValue = variance.evaluate(values);
		double standardDeviationValue = standardDeviation.evaluate(values);
		double skewnessValue = skewness.evaluate(values);
		double kurtosisValue = kurtosis.evaluate(values);
		
		System.out.println("samples="+values.length);
		System.out.println("mean="+meanValue);
		System.out.println("variance="+varianceValue);
		System.out.println("standardDeviation="+standardDeviationValue);
		System.out.println("skewness="+skewnessValue);
		System.out.println("kurtosis="+kurtosisValue);
		
		// 峰度 参考值 1.917
		// kurtosis=-0.6522074544292367
		// 两个值相差很多
		
		double sumValue = 0;
		for(double item:values){
			sumValue = sumValue + Math.pow((meanValue - item), 4);
		}
		
		double kurtosisValue2 = sumValue / (values.length - 1) / Math.pow(standardDeviationValue, 4);
		System.out.println("kurtosisValue2="+kurtosisValue2);
		// kurtosisValue2=1.9169777521295075
        // 这个值和网上查的值一致
		
		
		// double value3 = sumValue /  Math.pow(standardDeviationValue, 4);
		// System.out.println("value3="+value3);

		System.out.println("mean="+Util.mean(values));

		double var =  Util.centreDistance(values, 2);
	
		System.out.println("var="+var);
        double std = Math.pow(var, 0.5);
		System.out.println("std="+std);

		
		// System.out.println(Math.pow(2, 3));
		// System.out.println(Math.pow(4, 0.5));


		
		
		double kurtosisValue3 = Util.centreDistance(values, 4) / Math.pow(Util.centreDistance(values, 2), 2);
		System.out.println("kurtosisValue3="+kurtosisValue3);



	
	}
	
	
}
